package com.welab.pandahelper.components;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.welab.pandahelper.R;

/**
 * 头部局
 */
public class HeaderView extends LinearLayout {

    private TextView mRightTv;
    private ImageView mLeftImv;


    public HeaderView(Context context) {
        super(context);
        init(null, 0);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.HeaderView, defStyle, 0);

        String titleText = a.getString(
                R.styleable.HeaderView_titleText);
        String rightText = a.getString(
                R.styleable.HeaderView_rightText);
        Drawable leftDrawable = null;
        if (a.hasValue(R.styleable.HeaderView_leftDrawable)) {
            leftDrawable = a.getDrawable(
                    R.styleable.HeaderView_leftDrawable);
            leftDrawable.setCallback(this);
        }
        a.recycle();
        View.inflate(getContext(), R.layout.layout_header_view, this);
        if (leftDrawable != null) {
            ImageView leftImv = findViewById(R.id.left_img);
            leftImv.setImageDrawable(leftDrawable);
            leftImv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) v.getContext()).finish();
                }
            });
        }
        if (!TextUtils.isEmpty(rightText)) {
            mRightTv = findViewById(R.id.right_txt);
            mRightTv.setText(rightText);
        }


    }

    public void setRightViewClickListener(OnClickListener l) {
        mRightTv.setOnClickListener(l);
    }

}